/*
 * @Description:
 * @Date: 2023-11-01 20:19:28
 * @LastEditTime: 2023-11-25 13:06:59
 */
import { reactive } from 'vue';
import { getGithubData } from '../service';
import useGithubStore from '@/store/github';
import { run } from 'node:test';
import { resolve } from 'node:path';
import { reject, set } from 'lodash';

export type GitHubItem = {
	project_id: number;
	influence: string;
	response: string;
	activity: string;
	trend: string;
	github: string;
	name: string;
	sort_id: number;
};

type GitHubType = {
	loading: boolean;
	dataSource: GitHubItem[];
	addData: () => void;
	page: number;
	page_size: number;
	total: number;
};
const initData = function () {
	return new Promise(resolve => {
		let data = {
			data: {
				list: [
					{
						project_id: 1,
						influence: '68.59',
						response: '10.94',
						activity: '52.48',
						trend: '150.00',
						github: '78.26',
						name: 'AdguardTeam/AdguardFilters'
					},
					{
						project_id: 2,
						influence: '50.00',
						response: '57.65',
						activity: '82.70',
						trend: '66.17',
						github: '62.92',
						name: 'airbytehq/airbyte'
					},
					{
						project_id: 3,
						influence: '24.30',
						response: '27.27',
						activity: '51.25',
						trend: '36.42',
						github: '33.92',
						name: 'alibaba/nacos'
					},
					{
						project_id: 4,
						influence: '41.81',
						response: '35.40',
						activity: '68.98',
						trend: '33.50',
						github: '43.47',
						name: 'angular/angular'
					},
					{
						project_id: 5,
						influence: '18.46',
						response: '20.98',
						activity: '65.53',
						trend: '30.89',
						github: '32.11',
						name: 'angular/components'
					},
					{
						project_id: 6,
						influence: '22.38',
						response: '20.69',
						activity: '49.20',
						trend: '30.06',
						github: '29.71',
						name: 'ankidroid/Anki-Android'
					},
					{
						project_id: 7,
						influence: '27.83',
						response: '26.23',
						activity: '35.76',
						trend: '35.12',
						github: '31.29',
						name: 'ansible/ansible'
					},
					{
						project_id: 8,
						influence: '46.71',
						response: '46.98',
						activity: '77.00',
						trend: '50.11',
						github: '53.84',
						name: 'ant-design/ant-design'
					},
					{
						project_id: 9,
						influence: '45.43',
						response: '40.93',
						activity: '52.43',
						trend: '49.36',
						github: '47.11',
						name: 'apache/airflow'
					},
					{
						project_id: 10,
						influence: '21.44',
						response: '16.55',
						activity: '44.19',
						trend: '33.69',
						github: '28.69',
						name: 'apache/apisix'
					},
					{
						project_id: 11,
						influence: '36.41',
						response: '36.44',
						activity: '42.08',
						trend: '25.40',
						github: '34.25',
						name: 'apache/arrow'
					},
					{
						project_id: 12,
						influence: '38.67',
						response: '34.71',
						activity: '48.98',
						trend: '47.31',
						github: '42.53',
						name: 'apache/beam'
					},
					{
						project_id: 13,
						influence: '33.51',
						response: '42.27',
						activity: '62.27',
						trend: '42.35',
						github: '43.66',
						name: 'apache/dolphinscheduler'
					},
					{
						project_id: 14,
						influence: '31.76',
						response: '55.29',
						activity: '78.11',
						trend: '42.43',
						github: '48.94',
						name: 'apache/doris'
					},
					{
						project_id: 17,
						influence: '19.08',
						response: '28.53',
						activity: '79.30',
						trend: '27.66',
						github: '35.59',
						name: 'apache/iceberg'
					},
					{
						project_id: 18,
						influence: '35.52',
						response: '54.87',
						activity: '67.09',
						trend: '40.99',
						github: '47.34',
						name: 'apache/pulsar'
					},
					{
						project_id: 19,
						influence: '31.03',
						response: '33.89',
						activity: '38.13',
						trend: '51.42',
						github: '39.14',
						name: 'apache/shardingsphere'
					}
				],
				total: 223
			}
		};
		setTimeout(() => {
			resolve(data);
		}, 50);
	});
};
export default function (): GitHubType {
	const githubStore = useGithubStore();

	const addData = () => {
		if (github.dataSource.length < github.total || github.dataSource.length === 0) {
			github.loading = true;
			github.page++;
			initData()
				.then(res => {
					console.log(res);

					const newData: GitHubItem[] = (res.data.list || []).map((item: GitHubItem, index: number) => ({
						...item,
						sort_id: (github.page - 1) * github.page_size + index
					}));
					github.total = res.data.total || 0;
					github.dataSource = [...github.dataSource, ...newData];
					githubStore.list = github.dataSource;
				})
				.finally(() => {
					github.loading = false;
				});
		}
	};

	const github = reactive<GitHubType>({
		loading: false,
		dataSource: [],
		addData,
		page: 0,
		page_size: 25,
		total: 0
	});
	console.log(github, '请求道的数据');

	return github;
}
